<template>
  <!-- 课程列表 -->
  <div>
    <div class="list-wrapper flex-wrap" v-if="!vertical">
      <div class="list-item" :style="isMark ? markPageStyle : null" v-for="(item, index) in list" :key="index" @click="toPage(item)">
        <div class="list-item-img">
          <div class="item-default-img flex-wrap flex-middle flex-center" :style="{
              background: 'url(' + item.imageHttpUrl + ') center no-repeat'
            }" v-if="item.imageHttpUrl">
            <div class="item-default-text ellipsis2" v-if="item.name">
              {{ item.name }}
            </div>
          </div>
          <div class="item-default-img flex-wrap flex-middle flex-center" v-else>
            <div class="item-default-text ellipsis2">{{ item.name }}</div>
          </div>
          <div v-if="item.studyStatus" :class="['list-item-status', 'status-' + item.studyStatus]">{{ getStatus(item.studyStatus) }}</div>
        </div>
        <div class="item-title fs-14 fc-333 ellipsis bold">
          {{ item.name }}
        </div>
        <div class="flex-wrap flex-text-mid space-between mt5 mb10">
          <span class="fs-12 fc-999 flex-item letter">{{item.studyUserNum || 0}}人已加入学习</span>
          <span class="fs-12 fc-999 flex-wrap flex-text-mid">
           <span :class="[item.regType == 1 ? 'primary-color' : 'yellow-color']">
             {{ dealRegType(item.regType) }}
           </span>
            <span v-if="item.regType" style="margin: 0 2px">|</span>
            共{{item.jobNum || 0 }}任务
          </span>
        </div>
        <div class="mark-btn" v-if="isMark" @click.stop="cancelMark(item)">取消收藏</div>
      </div>
    </div>
    <div class="list-wrapper flex-wrap vertical" v-else>
      <div class="list-item flex-wrap flex-middle" v-for="(item, index) in list" :key="index" @click="toPage(item)">
        <div class="list-item-img">
          <div class="item-default-img flex-wrap flex-middle flex-center" :style="{
              background: 'url(' + item.imageHttpUrl + ') center no-repeat'
            }" v-if="item.imageHttpUrl">
            <div class="item-default-text ellipsis2" v-if="item.name">
              {{ item.name }}
            </div>
          </div>
          <div class="item-default-img flex-wrap flex-middle flex-center" v-else>
            <div class="item-default-text ellipsis2">{{ item.name }}</div>
          </div>
          <div v-if="item.studyStatus" :class="['list-item-status', 'status-' + item.studyStatus]">
            {{ getStatus(item.studyStatus) }}
          </div>
        </div>
        <div class="list-right flex-item">
          <div class="item-title fs-16 fc-333 ellipsis2 bold">
            <span class="abs-tip-T">培训项目</span>
            {{ item.name }}
          </div>
          <div class="item-icon-wrap flex-wrap flex-text-mid">
<!--             <p class="fs-12 fc-999">时间：{{ item.startDate }}</p> -->
            <div class="progress-wrap">
              <div class="green-bar" :style="{ width: `${getNum(item.passNum, item.jobNum)}%` }"></div>
            </div>
            <div class="progress-number flex-item fs-10">
              已学习 {{ getNum(item.passNum, item.jobNum) + "%" }}
            </div>
          </div>
        </div>
        <div class="list-bottom mt10 flex-wrap flex-text-mid">
          <p class="fs-12 fc-999">{{item.categoryName}}</p>
          <span class="split-line" v-if="item.categoryName"></span>
          <p class="fs-12 fc-999">共{{item.jobNum || 0}}任务</p>
          <span class="split-line" v-if="item.categoryName || item.jobNum"></span>
          <p class="fs-12 fc-999">{{ item.studyUserNum || 0 }} 人加入学习</p>
        </div>
      </div>
    </div>
  </div>
</template>


<script setup>
import { ref, computed,getCurrentInstance } from 'vue'
import { useStore } from 'vuex'
import { trainingStatus } from '@/assets/js/constant.js'
import { dealRegType } from '@/assets/js/util'
	const {
		$api,
		$uniPush,
		$toast,
	} = getCurrentInstance().proxy
// Props
const props = defineProps({
  list: {
    type: Array,
    default: () => []
  },
  vertical: {
    // 竖版显示
    type: Boolean,
    default: false
  },
  pageKey: {
    // 跳转id的key
    type: String,
    default: ""
  },
  progress: {
    // 是否显示课程学习进度（我的课程有效）
    type: Boolean,
    default: false
  },
  hideReco: {
    type: Boolean,
    default: false
  },
  isMark: {
    type: Boolean,
    default: false
  }
})

// Emits
const emit = defineEmits(['reload-list'])


// Refs
const markPageStyle = ref({
  padding: '6px 8px 10px',
  borderRadius: '4px',
  backgroundColor: '#fff',
  marginBottom: '10px'
})

const trainingStatusList = ref([
  { name: '未开始', value: '1' },
  { name: '学习中', value: '2' },
  { name: '已学完', value: '3' },
  { name: '已学完', value: '4' },
  { name: '已学完', value: '5' }
])

// Methods
const getNum = (i = 0, t = 0) => {
  let num = i / t
  return num ? Math.round(num * 10000) / 100 : "0.00"
}

const getStatus = (status) => {
  const current = trainingStatusList.value.find(e => e.value == status)
  return current ? current.name : ""
}

const toPage = (item) => {
  /*$uniPush({
    name: 'training-detail-id',
    params: {
      id: item.planId
    }
  })*/
  if (item.planType === '3') {
    $uniPush({
      name: 'training-atlas-id',
      params: {
        id: item.planId
      }
    })
  }else {
    $uniPush({
      name: 'training-detail-id',
      params: {
        id: item.planId
      }
    })
  }
}

const cancelMark = (row) => {
  $api.course.delCourseMark({ courseId: row.planId, courseType: 'T' })
    .then(() => {
      $toast.success('取消收藏成功')
      emit('reload-list')
    })
    .catch((e) => {
      $toast.fail(`取消收藏失败${e.msg ? "：" + e.msg : ""}`)
    })
}


</script>

<style lang="scss" scoped>
.list-wrapper {
  flex-wrap: wrap;
  padding-bottom: 4px;
  &.vertical {
    .list-item {
      width: 100%;
      padding: 15px 0;
      align-items: flex-start;
      flex-wrap: wrap;
      &:nth-child(2n) {
        margin-left: 0;
      }
      .list-item-img {
        width: 38.4vw;
        height: 21.6vw;
        margin-bottom: 0;
        .item-default-img {
          min-width: 38.4vw;
          width: 38.4vw;
          height: 21.6vw;
          background-color: #eeeeee !important;
        }
      }
      .item-icon-wrap {
        height: 30px;
        line-height: 15px;
        margin: 2px 0 8px;
        p {
          margin-top: 4px;
        }
        .progress-wrap {
          position: relative;
          width: 60px;
          height: 6px;
          background: #eeeeee;
          border-radius: 3px;
        }
        .green-bar {
          background: var(--color);
          left: 0;
          bottom: 0;
          position: absolute;
          height: 6px;
          border-radius: 3px;
        }
        .progress-number {
          color: #999999;
          font-size: 13px;
          bottom: 2px;
          width: 100%;
          text-align: left;
          padding: 0 5px;
        }
      }
      .list-right {
        width: calc(100% - 38.4vw - 9px);
        margin-left: 9px;
        min-height: 21.6vw;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .item-icon-wrap {
          height: 20px;
          margin-bottom: 0;
          .item-icon {
            font-size: 10px;
            color: #7f7f7f;
            height: 16px;
            line-height: 16px;
            padding: 0 4px;
            background: #f5f5f5;
            border-radius: 2px;
            & + .item-icon {
              margin-left: 4px;
            }
          }
        }
        .progress-bar {
          height: 20px;
          border-radius: 0 0 2px 2px;
          position: absolute;
          bottom: 0;
          width: 100%;
          .black-bar {
            background: #000000;
            opacity: 0.39;
            height: 100%;
          }
          .green-bar {
            background: var(--color);
            opacity: 0.74;
            height: 100%;
            bottom: 0;
            position: absolute;
          }
          .progress-number {
            color: #fff;
            font-size: 0.32rem;
            bottom: 2px;
            width: 100%;
            position: absolute;
            text-align: center;
          }
        }
      }
      .list-bottom {
        width: 100%;
      }
    }
  }
  .list-item {
    width: calc((100% - 13px) / 2);
    padding: 6px 0 10px;
    position: relative;
    &:nth-child(2n) {
      margin-left: 13px;
    }
    .list-item-img {
      position: relative;
      width: 100%;
      height: 24.8vw;
      margin-bottom: 5px;
      .item-default-img {
        width: 100%;
        height: 24.8vw;
        background: url('/static/image/course-img.png') no-repeat;
        background-color: #eeeeee !important;
        background-size: contain !important;
        .item-default-text {
          font-size: 12px;
          color: #fff;
          text-align: center;
          padding: 0 10px;
        }
      }
      .list-item-status {
        padding: 0 4px;
        height: 18px;
        line-height: 18px;
        font-size: 12px;
        font-weight: 400;
        color: #ffffff;
        border-radius: 24px 24px 2px 24px;
        position: absolute;
        left: 3px;
        top: 3px;
        background: var(--color);
        &.status-1 {
          background: #999999;
        }
        &.status-3,
        &.status-4,
        &.status-5 {
          background: #4da9fb;
        }
      }
    }
    .item-icon-wrap {
      height: 20px;
      .item-icon {
        font-size: 10px;
        color: #7f7f7f;
        height: 16px;
        line-height: 16px;
        padding: 0 4px;
        background: #f5f5f5;
        border-radius: 2px;
        & + .item-icon {
          margin-left: 4px;
        }
      }
    }
    .item-data {
      height: 25px;
      .item-data-item {
        & + .item-data-item:before {
          content: '|';
          margin: 0 6px;
        }
      }
    }
  }
  .mark-btn {
    width: 100%;
    height: 32px;
    border: 1px solid #EEEEEE;
    font-size: 14px;
    font-weight: 400;
    color: #999999;
    line-height: 32px;
    text-align: center;
    background-color: #fff;
  }
  .letter {
    letter-spacing: -1px
  }
}
</style>
